package edu.nf.project.webs.storeOperation;

import edu.nf.project.entitys.entity.storeOperation.convention.operate.CustomerOrderItem;
import edu.nf.project.entitys.entity.storeOperation.convention.operate.PayHistory;
import edu.nf.project.services.exception.derivedAnomalies.CustomerOrderException;
import edu.nf.project.services.response.BaseController;
import edu.nf.project.services.response.ResultVO;
import edu.nf.project.services.storeOperation.PayHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 支付记录控制器
 */
@RestController
@RequestMapping("/api/payHistory")
public class PayHistoryController extends BaseController {
    @Autowired
    private PayHistoryService payHistoryService;

    /**
     * 添加一条支付记录
     */
    @PostMapping("/addOne")
    public ResultVO<Boolean> newHistory(@RequestBody PayHistory payHistory) {
        try {
            Boolean flag = payHistoryService.newHistory(payHistory);
            return success(flag);
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            System.out.println(e);
            throw new CustomerOrderException(90000, "系统错误，请稍后重试");
        }
    }

    /**
     * 按用户查询相关支付记录
     */
    @GetMapping("/getAllForId")
    public ResultVO<List<PayHistory>> getHistory(@RequestParam Integer u_id) {
        try {
            List<PayHistory> payHistories = payHistoryService.getHistory(u_id);
            return success(payHistories);
        } catch (Exception e) {
            System.out.println(e);
            throw new CustomerOrderException(90000, "系统错误，请稍后重试");
        }
    }

    /**
     * 批量添加订单菜品项
     */
    @PostMapping("/addOrderItems")
    public ResultVO<Boolean> addOrderItems(@RequestBody List<CustomerOrderItem> items) {
        try {
            Boolean flag = payHistoryService.addOrderItems(items);
            return success(flag);
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            System.out.println(e);
            throw new CustomerOrderException(90001, "批量添加订单项失败，请稍后重试");
        }
    }
}
